<template>
  <div>
    <el-dialog title="分配角色" :visible.sync="buttontype" @close="fun(tname)">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        v-if="deptarr && form"
      >
        <el-form-item label="选择角色">
          <el-select v-model="form.roleIds" placeholder="请选择">
            <el-option
              v-for="item in deptarr.treeData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="form.account"></el-input> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="upuser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { http, getrole, setrole } from "../../api/api";
export default {
  props: ["type", "fun", "tname", "tablerow"],
  data() {
    return {
      buttontype: this.type, //模态框状态
      form: {
        //数据
        roleIds: this.tablerow.roleid,
      },
      deptarr: "", //部门信息
    };
  },
  mounted() {
    this.getrole();
  },
  methods: {
    upuser() {
      //  设置角色
      var type = true;
      for (var i in this.formtype) {
        if (this.form[i] === "") {
          type = false;
        }
      }
      // 修改数据发送验证
      if (type) {
        this.form.userId = this.tablerow.id;
        this.$http.get(http + setrole, { params: this.form }).then(
          (data) => {
            console.log(data.data);
            if (data.data.msg == "成功") {
              this.buttontype = false;
              this.$message({
                showClose: true,
                message: "恭喜你，设置角色成功",
                type: "success",
              });
            } else {
              this.$message.error(data.data.message);
            }
          },
          (error) => {
            /*  window.location = "./login.html"; */
            this.$message.error(error.data.message);
          }
        );
        delete this.form.userId;
      } else {
        this.$message.error("请补全信息");
      }
    },
    //  获取角色信息
    getrole() {
      var id = this.$store.state.user.info.profile.id;
      this.$http.get(http + getrole, { params: { idUser: id } }).then(
        (data) => {
          if (data.data.msg == "成功") {
            this.deptarr = data.data.data;
            console.log(this.deptarr);
          } else {
            this.$message.error(data.data.data);
          }
          console.log(data.data.message);
        },
        (err) => {
          this.$message.error(err.data.message);
        }
      );
    },
  },
  watch: {
    type() {
      this.buttontype = this.type;
    },
    /*  tablerow() {
      var json = {};
      for (var i in this.tablerow) {
        this.$set(json, i, this.tablerow[i].toString());
      }
      this.form = json;
    }, */
    tablerow() {
      this.form.rolelds = this.tablerow.roleid;
    },
  },
};
</script>

<style>
</style>